package com.qianbei.common.web;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.android.volley.Request;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.web.WebViewFragment.MyWebViewListener;
import com.qianbei.share.ui.ShareFragmentDialog;

import java.util.ArrayList;

import static com.qianbei.common.init.SetData.getCareer;
import static com.qianbei.common.init.SetData.getFace;

public class WebActivity extends BaseActivity implements MyWebViewListener {

    public WebViewFragment wv;
    public static WebBackListener mWbback;

    public static void startWebActivity(Context context, String url, WebBackListener wbback) {
        Intent in = new Intent(context, WebActivity.class);
        in.putExtra("url", url);
        context.startActivity(in);
        mWbback = wbback;
    }

    public static void startWebActivity(Context context, String url) {
        startWebActivity(context, url, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kit_web_activity);
        init();
    }



    private void init() {

        String url = getIntent().getStringExtra("url");

        if (url != null) {

            wv =   WebViewFragment.newInstance(url,false);
            wv.webListener = this;
            showFragment(wv, R.id.contentframe);
        }
//        showFragment(wv, R.id.contentframe);


        String data = getIntent().getStringExtra("data");
        if (data != null) {
            wv =   WebViewFragment.newInstance(data);
            wv.webListener = this;
            showFragment(wv, R.id.contentframe);

        }

//        showShareMenu();
    }

    @Override
    public void loadTitle(String title) {
        setTitle(title);
    }

    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.getBridgeWebView().canGoBack()) {
            wv.getBridgeWebView().goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 实现 baseFragment 的事件通知 ，子类如果需要处理 从写此方法 集中处理 不需要子类 实现此接口
     */
    @Override
    public void fClick(int id) {

        switch (id) {
            case R.id.title_left:

                if (mWbback != null) {
                    mWbback.back();
                }

                if (wv.getBridgeWebView().canGoBack()){
                    wv.getBridgeWebView().goBack();
                }else{
                    finish();
                }
                break;
            case R.id.title_right:
//                wv.shareAction();

                ArrayList<Integer> menuItem = new ArrayList<Integer>();
                menuItem.add(R.string.share_webo);
                menuItem.add(R.string.share_wei_xin);
                menuItem.add(R.string.share_friend);

                menuItem.add(R.string.cancel);
//
//                String title = shareJson.optString("title");
//                String subtitle = shareJson.optString("subtitle");
//                String linkUrl = shareJson.optString("linkUrl");
//                String imgUrl = shareJson.optString("imgUrl");

//                NewShareFragmentDialog dialogs = NewShareFragmentDialog.newInstance(""
//                        , "", "", "", menuItem);
//                dialogs.show(getFragmentManager(), "CloseAQDialog");


                ShareFragmentDialog dialogs = ShareFragmentDialog.newInstance("我是"+ SetData.getUserName()+"("+getCareer() +")"+"来「前辈」向我提问","前辈 — 向专业人士提问，获取一对一个性化答案。",String.format(PERSONAL_URL,
                        SetData.getUserID()),URL + getFace(),"web",false);
                dialogs.show(getFragmentManager(), this.getClass().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mWbback != null) {
            mWbback.back();
        }
    }


    public interface WebBackListener {
        void back();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        wv.refreshWebView();
    }

    /**
     * 显示分享按钮
     *
     */
    public void showShareMenu(){
        mTitle.setRightImage(R.drawable.persen_collect_share);
    }
}
